package com.stu.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Maps;
import com.stu.utils.XmlUtils;
import io.swagger.annotations.Api;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@Api(tags = "测试")
@RestController
@RequestMapping("/a")
public class AController {

    @PostMapping("jsonxml")
    public LocalDate jsonXml(@RequestBody String content) {
        JSONObject jsonObject = JSON.parseObject(content);
        System.out.println(jsonObject);
        Set<String> strings = jsonObject.keySet();
        jsonObject.forEach((k,v)->{
            log.info("key=[{}],value=[{}]",k,v);
        });

        return LocalDate.now();
    }

    @Data
    public static class Demo {

        private String id;
        private String name;
    }

    public static void main(String[] args) throws IOException {
        Demo demo = new Demo();
        demo.setId("1");
        demo.setName("demo");
        System.out.println(XmlUtils.jacksonBean2XML(demo));

        String s = FileUtils.readFileToString(new File("/Users/zhuzi/1.txt"),StandardCharsets.UTF_8);
        Demo demo1 = XmlUtils.jacksonXML2Bean(s,Demo.class);
        System.out.println(demo1);
        JSONObject msg = new JSONObject().fluentPut("msg",s);
        System.out.println(msg.toJSONString());
        System.out.println(XmlUtils.jacksonXML2Bean(msg.getString("msg"),Demo.class));
    }

    @RequestMapping(value = "/upload2")
    @ResponseBody
    public Map<String,Object> upload2(HttpServletRequest request,@RequestBody UploadEventData eventData) {
        List<EventData> eventDatas = null;
        log.info("请求参数[{}]",eventData);
        try {
            String items = JSONObject.toJSONString(eventData.getItems());
            log.info("参数[{}]",items);
            eventDatas = JSON.parseObject(items,new TypeReference<List<EventData>>() {});
            log.info("event=[{}]",eventDatas);
        } catch (Exception e) {
        }

        return Maps.newConcurrentMap();
    }

    @Data
    @Accessors(chain = true)
    public static class UploadEventData {

        private List<InputEventData> items;
    }

    @Data
    @Accessors(chain = true)
    public static class Attstr {

        private String activityId;
        private String eleUuid;
        private int pvtimes;
        private String apm;
    }

    @Data
    @Accessors(chain = true)
    public static class EventData implements Serializable {

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.id
         *
         * @mbggenerated
         */
        private Long id;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.eventid
         *
         * @mbggenerated
         */
        private String eventid;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.eventtime
         *
         * @mbggenerated
         */
        private Date eventtime;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.uploadtime
         *
         * @mbggenerated
         */
        private Date uploadtime;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.ipv4
         *
         * @mbggenerated
         */
        private String ip;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.ipv6
         *
         * @mbggenerated
         */
        private String ipv6;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.latitude
         *
         * @mbggenerated
         */
        private String latitude;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.longitude
         *
         * @mbggenerated
         */
        private String longitude;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.location_type
         *
         * @mbggenerated
         */
        private String location_type;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.country
         *
         * @mbggenerated
         */
        private String country;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.provcode
         *
         * @mbggenerated
         */
        private String provcode;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.citycode
         *
         * @mbggenerated
         */
        private String citycode;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.addraddress
         *
         * @mbggenerated
         */
        private String addraddress;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.useruuid
         *
         * @mbggenerated
         */
        private String useruuid;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.distinctid
         *
         * @mbggenerated
         */
        private String distinctid;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.distinct_id_type
         *
         * @mbggenerated
         */
        private String distinct_id_type;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.userid
         *
         * @mbggenerated
         */
        private String userid;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.roleid
         *
         * @mbggenerated
         */
        private Short roleid;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.nickname
         *
         * @mbggenerated
         */
        private String nickname;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.authstatus
         *
         * @mbggenerated
         */
        private Short authstatus;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.picture
         *
         * @mbggenerated
         */
        private String picture;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.apptype
         *
         * @mbggenerated
         */
        private String apptype;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.devicetype
         *
         * @mbggenerated
         */
        private String devicetype;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.channel
         *
         * @mbggenerated
         */
        private String channel;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.app_version
         *
         * @mbggenerated
         */
        private String app_version;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.manufacturer
         *
         * @mbggenerated
         */
        private String manufacturer;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.model
         *
         * @mbggenerated
         */
        private String model;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.wifi
         *
         * @mbggenerated
         */
        private Boolean wifi;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.os
         *
         * @mbggenerated
         */
        private String os;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.os_version
         *
         * @mbggenerated
         */
        private String os_version;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.screen_height
         *
         * @mbggenerated
         */
        private Integer screen_height;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.screen_width
         *
         * @mbggenerated
         */
        private Integer screen_width;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.carrier
         *
         * @mbggenerated
         */
        private String carrier;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.network_type
         *
         * @mbggenerated
         */
        private String network_type;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.eventtype
         *
         * @mbggenerated
         */
        private String eventtype;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.pageid
         *
         * @mbggenerated
         */
        private String pageid;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.pageurl
         *
         * @mbggenerated
         */
        private String pageurl;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.sourcepageid
         *
         * @mbggenerated
         */
        private String sourcepageid;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.targetPageid
         *
         * @mbggenerated
         */
        private String targetPageid;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.sourceeventid
         *
         * @mbggenerated
         */
        private String sourceeventid;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.moudleid
         *
         * @mbggenerated
         */
        private String moudleid;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.concernid
         *
         * @mbggenerated
         */
        private Integer concernid;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database column cm_event_data.attstr
         *
         * @mbggenerated
         */
        private String attstr;

        /**
         * This field was generated by MyBatis Generator. This field corresponds to the database table cm_event_data
         *
         * @mbggenerated
         */
        private static final long serialVersionUID = 1L;

        private String city;

        private String refer;

        /****
         * 资讯id
         */
        private String postid;

        /***
         * 资讯uuid
         */
        private String postuuid;

        /****
         * 会话id
         */
        private String sid;
    }

    @Data
    @Accessors(chain = true)
    public static class InputEventData {

        /***
         * 事件id
         */
        private String eventId;
        /***
         * 事件时间
         */
        private String eventTime;
        /**
         * 上报时间
         */
        private String uploadTime;
        /***
         * ipv4
         */
        private String ip;
        /**
         * ipV6
         */
        private String ipV6;
        /***
         * 位置信息-纬度
         */
        private String latitude;
        /**
         * 位置信息-经度
         */
        private String longitude;
        /***
         * 位置信息-类型
         */
        private String location_type;
        /**
         * 国家
         */
        private String country;
        /***
         * 省
         */
        private String provCode;
        /**
         * 城市
         */
        private String cityCode;

        private String city;
        /***
         * 详细地址
         */
        private String addraddress;
        /**
         * 用户唯一id
         */
        private String userUuid;
        /***
         * 设备id
         */
        private String distinctId;
        /**
         * 设备id类型
         */
        private String distinct_id_type;
        /***
         * 用户id
         */
        private String userId;
        /**
         * 角色id
         */
        private String roleId;
        /***
         * 昵称
         */
        private String nickname;
        /**
         * 认证状态
         */
        private String authStatus;
        /***
         * 头像
         */
        private String picture;
        /**
         * 应用类型
         */
        private String appType;
        /***
         * 设备类型 客户端类型
         */
        private String deviceType;
        /**
         * 渠道
         */
        private String channel;
        /***
         * 版本号
         */
        private String app_version;
        /**
         * 设备制造商
         */
        private String manufacturer;
        /***
         * 设备型号
         */
        private String model;
        /**
         * 是否wifi
         */
        private String wifi;
        /***
         * 操作系统
         */
        private String os;
        /**
         * 操作系统版本
         */
        private String os_version;
        /***
         * 屏幕高度
         */
        private String screen_height;
        /**
         * 屏幕宽度
         */
        private String screen_width;
        /***
         * 运营商
         */
        private String carrier;
        /***
         * 网络类型
         */
        private String network_type;
        /**
         * 事件类型
         */
        private String eventType;
        /***
         * 当前页面id
         */
        private String pageId;
        /**
         * 当前页面url
         */
        private String pageUrl;
        /***
         * 来源页面id
         */
        private String sourcePageId;
        /**
         * 目标页面id
         */
        private String targetPageId;
        /***
         * 当前页面曝光时间id
         */
        private String sourceEventId;
        /**
         * 模块id
         */
        private String moudleId;
        /***
         * 学科id
         */
        private String concernId;

        /****
         * 资讯id
         */
        private String postId;

        /***
         * 资讯uuid
         */
        private String postuuid;

        /****
         * 会话id
         */
        private String sid;
    }

    @RequestMapping(value = "/upload")
    @ResponseBody
    public Map<String,Object> upload(HttpServletRequest request,String items) {

        List<EventData> eventData = null;
        try {
            //ListenableFuture<SendResult<String, String>> sendResult = this.kafkaTemplate.sendDefault("{\"items\":" + items + "}");
            //sendResult.addCallback(new SendResultListenableFutureCallback());
            eventData = JSON.parseObject(items,new TypeReference<List<EventData>>() {});
        } catch (Exception e) {

        }

        return Maps.newConcurrentMap();
    }


/*    2022-06-19 00:41:30[cn.com.cmt.media.controller.DefaultExceptionHandler:163][resin-port-8088-25]-全局异常补获:

    {
        "reqParam":{
    },"reqUA":"Mozilla/5.0 (iPhone; CPU iPhone OS 15_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.23(0x1800172a) NetType/4G Language/zh_CN", "reqURL":"http://apps.medtrib.cn/app/event/post2.json"
    }

    org.springframework.http.converter.HttpMessageNotReadableException:
    Required request body is   missing:
    public java.util.Map<java.lang.String, java.lang.Object> cn.com.cmt.media.controller.app.data.AppEventUploadController.postOpenUpload(javax.servlet.http.HttpServletRequest,cn.com.cmt.media.app.event.entity.UploadEventData)
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:154)
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:128)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:122)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:157)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:127)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)*/
}
